<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['grn'][$_SESSION['s_userType'.$cfgPack]])
      && $cfgFileNames['grn'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  if(isset($_POST['deliveryChallanDetailId']))
  {  
    foreach ($_POST['deliveryChallanDetailId'] as $key => $value)
    {  	
    	$quantityPosted = $_POST['qtyTaken'][$key];
  	  $selectItemIdQuery = "SELECT itemId
                              FROM deliverychallandetail
                             WHERE deliveryChallanDetailId = ".$value;
      $selectItemIdQueryResult = mysql_query($selectItemIdQuery);
      $count = 0;
      while($itemRow = mysql_fetch_array($selectItemIdQueryResult))
      { 
        $itemId  = $itemRow['itemId'];
        
        //Update deliverychallandetail table : Start
        $updateDetailQty = "UPDATE deliverychallandetail 
                               SET quantity = quantity - ".$quantityPosted."
                             WHERE deliveryChallanDetailId = ".$value;
        $updateDetailQtyRes = mysql_query($updateDetailQty);
        if(!$updateDetailQtyRes)
          die("Update deliverychallandetail : ".mysql_error());
        //Update deliverychallandetail table : End
        
        //Update Item table : Start
        $updateItemQty = "UPDATE item
                             SET currentStock = currentStock + ".$quantityPosted."
                           WHERE itemId =".$itemRow['itemId'];
        $updateItemQtyRes = mysql_query($updateItemQty);
        if(!$updateItemQtyRes)
          die("Update item : ".mysql_error());
        //Update Item table : End

        //Insert goodsreturn table : Start
        $currentDate = date("Y-m-d");
        $insertGoodsReturn = "INSERT INTO goodsreturn (deliveryChallanDetailId,returnDate,quantityReturn)
                              VALUES (".$value.",'".$currentDate."',".$quantityPosted.")";
        $insertGoodsReturnRes = mysql_query($insertGoodsReturn);
        if(!$insertGoodsReturnRes)
          die("Insert Query Not Inserted : ".mysql_error());
        //Insert goodsreturn table : End
        
        $count++;
      }
    }
  }
    
  ///// Select Party : Start
  $partyId       = array();
  $supId         = array();
  $sCompanyName   = array();
  $pCompanyName   = array();
  
   $selectParty = "SELECT DISTINCT 0 AS sid,deliverychallanmaster.partyId AS pid,party.partyId,party.companyName
                     FROM deliverychallanmaster
                     JOIN party ON party.partyId = deliverychallanmaster.partyId
                    ORDER BY party.companyName";
  $selectPartyResult = mysql_query($selectParty);
  $b = 0;
   while($partyRow = mysql_fetch_array($selectPartyResult))
  { 
    // comment becoz if we require in future 
    //$partyId[$b]     = $partyRow['id'];
    $partyId[$b]     = $partyRow['pid']."P";
    $pCompanyName[$b] = $partyRow['companyName'];
    $b++;
  }
  if($selectPartyResult == 0)
    {
	    $msg = 'Record Not Found...!';
    }
  	}
  ///// Select Party : End
  include("./bottom.php");
  $smarty->assign("partyId",$partyId);
  $smarty->assign("supId",$supId);
  $smarty->assign("sCompanyName",$sCompanyName);
  $smarty->assign("pCompanyName",$pCompanyName);
  $smarty->display("grn.tpl");

?>